package com.accp.demo.service.purchase;

import static org.hamcrest.CoreMatchers.nullValue;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import com.accp.demo.mapper.pruchase.IPurchaseInquiryMapper;
import com.accp.demo.pojo.PageBean;
import com.accp.demo.pojo.purchase.PurchaseInquiry;
import com.accp.demo.pojo.purchase.PurchaseInquiryDetail;
import com.accp.demo.util.TestUtil;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonAnyFormatVisitor;

@Service
public class PurchaseInquiryService {
	
	@Autowired
	private IPurchaseInquiryMapper iPurchaseInquiryMapper;
	
	/**
	 * 主从新增采购询价
	 * @param inquiry
	 * @return
	 */
	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
	public boolean savePurchaseInquiryBiz(Map<String, Object> map ) {
		try {
			System.out.println(map.get("createTime").toString().isEmpty());
			if(map.get("createTime").toString().isEmpty()){
				//新增
				iPurchaseInquiryMapper.savePurchaseInquiry(map);
				System.out.println("新增");
			}else{
				//修改
				System.out.println("修改");
				iPurchaseInquiryMapper.updateInquiry(map);
				iPurchaseInquiryMapper.deleteDateil(map);
				System.out.println(iPurchaseInquiryMapper.deleteDateil(map));
				iPurchaseInquiryMapper.savePurchaseInquiryDateil(map);
				System.out.println(iPurchaseInquiryMapper.savePurchaseInquiryDateil(map));
				System.out.println("修改");
			}
		} catch (Exception e) {
			return false;
		}
		return true;
	}
	
//	/**
//	 * 修改主从表信息
//	 * @param inquiry
//	 * @return
//	 */
//	public void updateInquiry(PurchaseInquiry inquiry){
//		if (inquiry.getPurchaseInquiryDetails()!=null){
//				iPurchaseInquiryMapper.deleteDateil(inquiry.getBillNo());
//		}
//		for (PurchaseInquiryDetail i : inquiry.getPurchaseInquiryDetails()) {
//			i.setBillNo(inquiry.getBillNo());
//			iPurchaseInquiryMapper.savePurchaseInquiryDateil(i);
//		}
//		System.out.println("==============");
//		System.out.println(inquiry);
//		System.out.println("==============");
//		iPurchaseInquiryMapper.updateInquiry(inquiry);
//	}
	
	/**
	 * 根据编号查询询价主表
	 * @param id
	 * @return
	 */
	public PurchaseInquiry findQueryInquiryBiz(String id) {
		//System.out.println(id);
		return iPurchaseInquiryMapper.queryInquiryInfo(id);
	}

	/**
	 * 根据询价主表的编号查询count
	 * @param id
	 * @return
	 */
	public Integer findQueryInquiryCount(String id) {
		//System.out.println(id);
		return iPurchaseInquiryMapper.queryInquiryCount(id);
	}

	/**
	 * 分页查询
	 * @param pagenum
	 * @param pagesize
	 * @return
	 */
	public PageBean findQueryInquiryPageBiz(Integer pagenum,Integer pagesize){
		System.out.println(pagenum);
		List<PurchaseInquiry> pi=iPurchaseInquiryMapper.queryInquiryPage(pagenum-1, pagesize);
		System.out.println(pi);
		PageBean pageBean=new PageBean(1, iPurchaseInquiryMapper.queryInquiryAllCount(), pagenum, pi);
		return pageBean;
	}
	/**
	 * 逻辑删除 主表
	 * @param id
	 * @param state
	 * @return
	 */
	public Boolean updateInquiryDel(Map<String, Object> map){
		if(iPurchaseInquiryMapper.updateInquiryDel(map)>0){
			return true;
		}
		return false;
	}
	/**
	 * 审核
	 * @param id
	 * @param state
	 * @return
	 */
	public Boolean updateInquirySH(Map<String, Object> map){
		if(iPurchaseInquiryMapper.updateInquirySH(map)>0){
			return true;
		}
		return false;
		
	}
	
	public String getNewNo(Map<String, Object> map){
		return TestUtil.getNewNo(this.iPurchaseInquiryMapper.queryMaxId(map));
	}
}
